package com.pingan.haofang.searchcloud.web.metadata.controller;

import com.pingan.haofang.framework.common.functional.lang.control.bean.BeanCopy;
import com.pingan.haofang.searchcloud.common.constants.WebConstants;
import com.pingan.haofang.searchcloud.common.validator.annotation.Valid;
import com.pingan.haofang.searchcloud.metadata.dto.MetadataFieldQueryResult;
import com.pingan.haofang.searchcloud.metadata.dto.MetadataFieldTypeQueryResult;
import com.pingan.haofang.searchcloud.metadata.dto.MetadataFieldsGenerateDTO;
import com.pingan.haofang.searchcloud.metadata.service.MetadataService;
import com.pingan.haofang.searchcloud.web.metadata.form.MetadataFieldsGenerateForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;


/**
 * Medatadata相关
 *
 * @author baojie371
 * @date 17/5/3
 * @since 1.0.0
 */
@RestController
@RequestMapping(value = WebConstants.WEB_PREFIX + "/metadata")
@Api(tags = "Index.Medatadata", description = "索引元数据管理接口")
public class MetadataController {

    @Autowired
    private MetadataService metadataService;

    /**
     * 字段类型查询
     *
     * @return
     */
    @RequestMapping(value = "allTypes", method = RequestMethod.GET)
    @ApiOperation("字段类型查询")
    public List<MetadataFieldTypeQueryResult> queryAllTypes() {
        return metadataService.findAllTypes();
    }

    /**
     * 根据rpc接口自动生成字段
     *
     * @param form
     * @return
     */
    @RequestMapping(value = "generateFields", method = RequestMethod.GET)
    @Valid
    @ApiOperation("根据rpc接口生成字段")
    public List<MetadataFieldQueryResult> generateFields(@ModelAttribute MetadataFieldsGenerateForm form) {

        MetadataFieldsGenerateDTO dto = BeanCopy.toFunction(MetadataFieldsGenerateDTO.class, BeanUtils::copyProperties)
                .apply(form);
        return metadataService.generateFields(dto);
    }
}
